/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package commons.util.graph;

import commons.util.ExceptionUtil;

/**
 * A directed edge from parent to child.
 *
 * @param <N> node data type
 * @param <E> edge data type
 * @author Susanta Tewari
 */
public class Edge<N, E> {

    /** Field description */
    private final Node<N, E> parent;

    /** Field description */
    private final Node<N, E> child;

    /** Field description */
    private E data;

    /**
     * Creates an edge from the parent (head) to the child (tail).
     *
     * @param parent parent node
     * @param child child node
     * @param data
     */
    public Edge(Node<N, E> parent, Node<N, E> child, E data) {

        if (parent == null) {
            ExceptionUtil.throwIllegalArgExNull("parent");
        }

        if (child == null) {
            ExceptionUtil.throwIllegalArgExNull("child");
        }

        if (data == null) {
            ExceptionUtil.throwIllegalArgExNull("data");
        }

        this.parent = parent;

        this.child  = child;

        this.data   = data;
    }

    /**
     * The parent (head) of this edge.
     *
     * @return the parent of this edge
     */
    public Node<N, E> getParent() {

        return parent;
    }


    /**
     * The child (tail) of this edge.
     *
     * @return the child of this edge
     */
    public Node<N, E> getChild() {

        return child;
    }


    /**
     * The data associated with this edge. Edge Data types are assumed to be immutable.
     *
     * @return edge data
     */
    public E getData() {

        return data;
    }


    /**
     * The data associated with this edge. Edge Data types are assumed to be immutable.
     *
     * @param data edge data
     */
    public void setData(E data) {

        this.data = data;
    }


    /**
     * {@code toString()} on its data.
     *
     * @return {@code toString()} on its data
     */
    @Override
    public String toString() {

        return data.toString();
    }
}
